﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;
using System.Data.SqlClient;
public partial class vote_VoteListAll : System.Web.UI.Page
{

    public string votes;
    //public string userid = "";
    public DataTable dt = new DataTable();
    public string more;
    public int pagesize=10;
    public string pager;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt.Columns.Add(new DataColumn("div"));
            dt.Columns.Add(new DataColumn("voters"));
            dt.Columns.Add(new DataColumn("img"));
            dt.Columns.Add(new DataColumn("name"));
        }
        if (Request["opration"] != null)
        {
            switch (Request["opration"].ToString())
            {
                
                case "vote":
                    Vote();
                    break;
                case "my":
                    VoteList(int.Parse(Request["num"].ToString()), "my");
                    break;
                case "all":
                    VoteList(int.Parse(Request["num"].ToString()), "all");
                    break;
            }
        }
    }

    public void VoteList(int page, string type)
    {
        string sSql = "";
        if (type == "all")

            sSql = "with a as ( select row_number() over(order by intime desc) as num,(select count(*) from t_vote ) as countnum,* from t_vote ) select * from a where num between " + ((page-1) * pagesize +1) + " and " + ((page-1) * pagesize + pagesize);
        else
            sSql = "with a as ( select row_number() over(order by intime desc) as num,(select count(*) from t_vote where inman='" + Session["userid"].ToString() + "' ) as countnum,* from t_vote where inman='" + Session["userid"].ToString() + "' ) select * from a where num between " + ((page-1) * pagesize+1) + " and " + ((page-1) * pagesize + pagesize);
           
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        SqlDataReader drr = myReader.FDataReader;
        int countnum = 0;
        while (drr.Read())
        {
            countnum = int.Parse(drr["countnum"].ToString());
            string div = "<table>";
            div += "<tr><td><a href='voteshow.aspx?id=" + drr["id"].ToString() + "'>" + drr["title"].ToString() + "</a></td></tr>";
            if (drr["description"].ToString() != "")
                div += "<tr><td>" + drr["description"].ToString() + "</td></tr>";

            sSql = "select * from t_vote_item where voteid='" + drr["id"].ToString() + "' and voteid not in ( select voteid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
            TSqlDBReader myReader1 = new TSqlDBReader(sSql);
            SqlDataReader drritem = myReader1.FDataReader;
            if (drritem.HasRows)
            {
                while (drritem.Read())
                {
                    if (drr["type"].ToString() == "1")
                        div += "<tr><td><input type='radio' name='" + drr["id"].ToString() + "' value='" + drritem["id"].ToString() + "' />" + drritem["content"].ToString() + "</td></tr>";
                    else
                        div += "<tr><td><input type='checkbox' name='" + drr["id"].ToString() + "'value='" + drritem["id"].ToString() + "' />" + drritem["content"].ToString() + "</td></tr>";
                }
                div += "<tr><td><input type='button' onclick=javascript:vote('" + drr["id"] + "') value='投票'></td></tr>";
            }
            drritem.Close();
            myReader1.Free();

            sSql = "select * from t_vote_item where voteid='" + drr["id"].ToString() + "' and id in ( select voteitemid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
            TSqlDBReader myReader2 = new TSqlDBReader(sSql);
            drritem = myReader2.FDataReader;
            if (drritem.HasRows)
            {

                while (drritem.Read())
                {
                    div += "<tr><td>" + drritem["content"].ToString() + "</td></tr>";
                }
            }
            div += "</table>";
            drritem.Close();
            myReader2.Free();

            DataRow dr = dt.NewRow();
            dr["div"] = div;
            dr["voters"] = drr["voters"];
            TUser ouser = new TUser(GetIdByEmail(drr["inman"].ToString()));
            ouser.Select();
            if (ouser.MyPic != "")
                dr["img"] = "<img width ='46px' height ='46px' src='..\\user\\pics\\" + ouser.MyPic + "' />";
            else
                dr["img"] = "<img width ='46px' height ='46px' src='..\\user\\pics\\user.jpg' />";
            dr["name"] = ouser.BigName;

            dt.Rows.Add(dr);
            ouser = new TUser(GetIdByEmail(drr["inman"].ToString()));

        }
        drr.Close();

        myReader.Free();
        if(  countnum % pagesize >0)
            pager = GenPagebar(page, countnum / pagesize+1);
        else
            pager = GenPagebar(page, countnum /pagesize );
        rpvote.DataSource = dt;
        rpvote.DataBind();

    }

    public string GetIdByEmail(string email)
    {
        string sSql = " select * from t_user where email='" + email + "' ";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        if (myReader.FDataReader.HasRows)
            myReader.FDataReader.Read();
        sSql = myReader.FDataReader["uid"].ToString();
        myReader.Free();
        return sSql;

    }

    public void Vote()
    {
        TVote oVote = new TVote(Request["vid"].ToString());
        oVote.Select();
        string[] items = Request["items"].ToString().Split(',');
        foreach (string str in items)
        {
            if (str != "")
            {
                TVoteUser oVoteUser = new TVoteUser();
                oVoteUser.Id = Guid.NewGuid().ToString();
                oVoteUser.UserId = Session["userid"].ToString();
                oVoteUser.VoteId = oVote.Id;
                oVoteUser.VoteItemId = str;
                oVoteUser.InMan = Session["userid"].ToString();
                oVoteUser.InTime = DateTime.Now;
                oVoteUser.Insert();
                TVoteItem oVoteItem = new TVoteItem(str);
                oVoteItem.Select();
                oVoteItem.Votes += 1;
                oVoteItem.Update();
            }
        }
        oVote.Voters += 1;
        oVote.Update();
        Server.Transfer("voteshow.aspx" + oVote.Id);
    }

    public string GenPagebar(int PageNum,int PageCounts)
    {
        string result = "<div class=\"pagebox\">";

       
        if (PageCounts > 1)
        {
            if (PageNum == 1)//第一页
            {
                result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
                result += "<span class=\"pagebox_next\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','2')\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','" + PageCounts.ToString() + "')\">尾页</a></span>";
            }
            if ((PageNum > 1) && (PageNum < PageCounts))
            {
                result += "<span class=\"pagebox_num\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','1')\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','" + (PageNum - 1).ToString() + "')\">上一页</a></span>";
                result += "<span class=\"pagebox_next\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','" + (PageNum + 1).ToString() + "')\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','" + PageCounts.ToString() + "')\">尾页</a></span>";
            }

            if (PageNum == PageCounts)//最后一页
            {
                result += "<span class=\"pagebox_num\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','1')\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a onclick=\"showlistall('" + Request["opration"].ToString() + "','" + (PageCounts - 1).ToString() + "')\">上一页</a></span>";
                result += "<span class=\"pagebox_next_nolink\">下一页</span>";
                result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
            }
        }
        else
        {
            result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
            result += "<span class=\"pagebox_num_nonce\"><a>1</a></span>";
            result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
            result += "<span class=\"pagebox_next_nolink\">下一页</span>";
            result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
        }

        result += "<span style=\"padding-top: 4px;\">&nbsp;(每页显示" + pagesize.ToString() + "行)</span>";
        result += "</div>";

        return result;
    }

   

    //public string Pager()
    //{

    //}

}
